<template>
	<view class="container">
		<u-navbar title-size="34" leftIconColor="#343A40" title-color="#343A40" :border-bottom="false"
			leftIcon="arrow-left" :custom-back="goBack" placeholder>
			<view class="slot-wrap ">
				<view class="search-wrap">
					<!-- 如果使用u-search组件，必须要给v-model绑定一个变量 -->
					<u-search v-model.trim="keyword" :show-action="true" height="70" bg-color="#ffffff" border-color="#D8D9E1"
						@custom="searchValue" action-text="搜索" :action-style="{color: '#292940'}"
						placeholder="搜索作品或者創作者" placeholder-color="#CED4DA"
						search-icon-color="#ADB5BD"></u-search>
				</view>
			</view>
		</u-navbar>
		<view class="search-box">
			<view class="padding-wrap bg-ffffff">
				<u-tabs :list="tabList" active-color="#292940" inactive-color="#292940" :is-scroll="false"
					font-size="30" bar-height="6" bar-width="24" :bar-style="activeColor" :current="current"
					@change="change"></u-tabs>
			</view>
		</view>
		<view class="order-wrap">
			<view v-show="current==0">
				<view class="order-item" @tap="toWait(item)" v-for="item in allList">
					<view class="d-flex justify-content-between align-items-center transaction-time">
						<view class="text-999BA8 fz-22">交易时间: {{item.created_at}}</view>
						<view class="text-999BA8 fz-22" v-if="item.status == 2">订单完成</view>
						<view class="text-999BA8 fz-22" v-if="item.status == -1">订单取消</view>
						<view class="text-999BA8 fz-22" v-if="item.status == -2">等待退款</view>
						<view class="text-999BA8 fz-22" v-if="item.status == -3">退款完成</view>
						<view class="d-flex align-items-center pay-time" v-if="item.status == 1">
							<view class="text-ffffff fz-24 text-center pay-tip">等待支付</view>
							<view class="count-down-box">
								<u-count-down  :hide-zero-day="true" :timestamp="getTime(item.created_at)" font-size="26" separator-size="26"
								 separator-color="#292940" color="#292940" bg-color="#F5F6F9" height="28"
								 :show-days="false" :show-hours="false" separator="zh" @end="allEnd"></u-count-down>
							</view>
						</view>
					</view>
					<view class="d-flex position-relative" v-if="item.blind_box_id === 0">
						<view class="order-img">
							<image :src="item.works.file" mode="aspectFill" v-if="item.works.type == 1"></image>
							<image :src="item.works.cover" mode="aspectFill" v-else></image>
						</view>
						<view class="order-right position-relative d-flex justify-content-between align-items-center">
							<view class="order-name">{{item.works.name}}#{{item.market.no}}</view>
							<view class="order-amount">
								<view class="amount">
									<span class="fz-30">￥</span>
									<span class="fz-40 font-DIN">{{item.amount}}</span>
								</view>
								<view class="count">
									x{{item.count}}
								</view>
							</view>
						</view>
					</view>
					<view class="d-flex position-relative" v-else>
						<view class="order-img">
							<image :src="item.box.cover_url" mode="aspectFill"></image>
						</view>
						<view class="order-right position-relative d-flex justify-content-between align-items-center">
							<view class="order-name">{{item.box.name}}</view>
							<view class="order-amount">
								<view class="amount">
									<span class="fz-30">￥</span>
									<span class="fz-40 font-DIN">{{item.amount}}</span>
								</view>
								<view class="count font-DIN">
									x{{item.count}}
								</view>
							</view>
						</view>
					</view>
					<view class="d-flex justify-content-end" v-if="item.status == 1">
						<view class="cancel-bg card-shadow text-666978 fz-26 text-center btn" @click.stop="cancel(item)">取消订单</view>
						<view class="yellow-bg card-shadow text-ffffff fz-26 text-center btn">立即支付</view>
					</view>
				</view>
				<u-loadmore :status="allStatus" v-show="allLoading" margin-top="50" />
				<u-empty v-show="allTotal === 0 && !allLoading" src="/static/home/nodata-icon.png" margin-top="200"></u-empty>
			</view>
			<view v-show="current==1">
				<view class="order-item" @tap="toWait(item)" v-for="item in buyList">
					<view class="d-flex justify-content-between align-items-center transaction-time">
						<view class="text-999BA8 fz-22">交易时间: {{item.created_at}}</view>
						<view class="d-flex align-items-center pay-time">
							<view class="text-ffffff fz-24 text-center pay-tip">等待支付</view>
							<view class="count-down-box">
								<u-count-down  :hide-zero-day="true" :timestamp="getTime(item.created_at)" font-size="26" separator-size="26"
								 separator-color="#292940" color="#292940" bg-color="#F5F6F9" height="28"
								 :show-days="false" :show-hours="false" separator="zh" @end="waitEnd"></u-count-down>
							</view>
						</view>
					</view>
					<view class="d-flex position-relative" v-if="item.blind_box_id === 0">
						<view class="order-img">
							<image :src="item.works.file" mode="aspectFill" v-if="item.works.type == 1"></image>
							<image :src="item.works.cover" mode="aspectFill" v-else></image>
						</view>
						<view class="order-right position-relative d-flex justify-content-between align-items-center">
							<view class="order-name">{{item.works.name}}#{{item.market.no}}</view>
							<view class="order-amount">
								<view class="amount">
									<span class="fz-30">￥</span>
									<span class="fz-40 font-DIN">{{item.amount}}</span>
								</view>
								<view class="count font-DIN">
									x{{item.count}}
								</view>
							</view>
						</view>
					</view>
					<view class="d-flex position-relative" v-else>
						<view class="order-img">
							<image :src="item.box.cover_url" mode="aspectFill"></image>
						</view>
						<view class="order-right position-relative d-flex justify-content-between align-items-center">
							<view class="order-name">{{item.box.name}}</view>
							<view class="order-amount">
								<view class="amount">
									<span class="fz-30">￥</span>
									<span class="fz-40 font-DIN">{{item.amount}}</span>
								</view>
								<view class="count font-DIN">
									x{{item.count}}
								</view>
							</view>
						</view>
					</view>
					<view class="d-flex justify-content-end">
						<view class="cancel-bg card-shadow text-666978 fz-26 text-center btn" @click.stop="cancel(item)">取消订单</view>
						<view class="yellow-bg card-shadow text-ffffff fz-26 text-center btn">立即支付</view>
					</view>
				</view>
				<u-loadmore :status="buyStatus" v-show="buyLoading" margin-top="50" />
				<u-empty v-show="buyTotal === 0 && !buyLoading" src="/static/home/nodata-icon.png" margin-top="200"></u-empty>
			</view>
			<view v-show="current==2">
				<view class="order-item" @tap="toWait(item)"  v-for="item in endList">
					<view class="d-flex justify-content-between align-items-center transaction-time">
						<view class="text-999BA8 fz-22">交易时间: {{item.created_at}}</view>
						<view class="text-999BA8 fz-22">订单完成</view>
					</view>
					<view class="d-flex position-relative" v-if="item.blind_box_id === 0">
						<view class="order-img">
							<image :src="item.works.file" mode="aspectFill" v-if="item.works.type == 1"></image>
							<image :src="item.works.cover" mode="aspectFill" v-else></image>
						</view>
						<view class="order-right position-relative d-flex justify-content-between align-items-center">
							<view class="order-name">{{item.works.name}}#{{item.market.no}}</view>
							<view class="order-amount">
								<view class="amount">
									<span class="fz-30">￥</span>
									<span class="fz-40 font-DIN">{{item.amount}}</span>
								</view>
								<view class="count font-DIN">
									x{{item.count}}
								</view>
							</view>
						</view>
					</view>
					<view class="d-flex position-relative" v-else>
						<view class="order-img">
							<image :src="item.box.cover_url" mode="aspectFill"></image>
						</view>
						<view class="order-right position-relative d-flex justify-content-between align-items-center">
							<view class="order-name">{{item.box.name}}</view>
							<view class="order-amount">
								<view class="amount">
									<span class="fz-30">￥</span>
									<span class="fz-40 font-DIN">{{item.amount}}</span>
								</view>
								<view class="count font-DIN">
									x{{item.count}}
								</view>
							</view>
						</view>
					</view>
				</view>
				<u-loadmore :status="endStatus" v-show="endLoading" margin-top="50" />
				<u-empty v-show="endTotal === 0 && !endLoading" src="/static/home/nodata-icon.png" margin-top="200"></u-empty>
			</view>
			<view v-show="current==3">
				<view class="order-item" @tap="toWait(item)" v-for="item in offList">
					<view class="d-flex justify-content-between align-items-center transaction-time">
						<view class="text-999BA8 fz-22">交易时间: {{item.created_at}}</view>
						<view class="text-999BA8 fz-22">订单取消</view>
					</view>
					<view class="d-flex position-relative" v-if="item.blind_box_id === 0">
						<view class="order-img">
							<image :src="item.works.file" mode="aspectFill" v-if="item.works.type == 1"></image>
							<image :src="item.works.cover" mode="aspectFill" v-else></image>
						</view>
						<view class="order-right position-relative d-flex justify-content-between align-items-center">
							<view class="order-name">{{item.works.name}}#{{item.market.no}}</view>
							<view class="order-amount">
								<view class="amount">
									<span class="fz-30">￥</span>
									<span class="fz-40 font-DIN">{{item.amount}}</span>
								</view>
								<view class="count font-DIN">
									x{{item.count}}
								</view>
							</view>
						</view>
					</view>
					<view class="d-flex position-relative" v-else>
						<view class="order-img">
							<image :src="item.box.cover_url" mode="aspectFill"></image>
						</view>
						<view class="order-right position-relative d-flex justify-content-between align-items-center">
							<view class="order-name">{{item.box.name}}</view>
							<view class="order-amount">
								<view class="amount">
									<span class="fz-30">￥</span>
									<span class="fz-40 font-DIN">{{item.amount}}</span>
								</view>
								<view class="count font-DIN">
									x{{item.count}}
								</view>
							</view>
						</view>
					</view>
				</view>
				<u-loadmore :status="offStatus" v-show="offLoading" margin-top="50" />
				<u-empty v-show="offTotal === 0 && !offLoading" src="/static/home/nodata-icon.png" margin-top="200"></u-empty>
			</view>
		</view>
		<u-modal v-model="modalShow" ref="uModal" :async-close="true" content="确定取消此订单吗？" show-cancel-button  @confirm="orderCancel" confirmColor="#00C0FF"></u-modal>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				modalShow:false,
				keyword:'',
				activeColor: { 'background': 'linear-gradient(141deg, #7A60FF 0%, #1EF7FE 100%)' },
				current: 0,
				tabList: [{
						name: '全部'
					},
					{
						name: '待付款'
					},
					{
						name: '已完成'
					},
					{
						name: '已取消'
					}
				],
				allPage:1,
				allStatus:'loading',
				allLoading:false,
				allTotal:'',
				allList: [],
				
				buyPage:1,
				buyStatus:'loading',
				buyLoading:false,
				buyTotal:'',
				buyList: [],
				
				endPage:1,
				endStatus:'loading',
				endLoading:false,
				endTotal:'',
				endList: [],
				
				offPage:1,
				offStatus:'loading',
				offLoading:false,
				offTotal:'',
				offList: [],
				
				all_keyword:'',
				buy_keyword:'',
				end_keyword:'',
				off_keyword:'',
				cancelItem:null,
				
			}
		},
		onLoad(options) {
			this.$nextTick(()=>{
				this.current = options.current
			})
			this.getAll(1)
			this.getBuy(1)
			this.getEnd(1)
			this.getOff(1)
		},
		onShow() {
			if(this.isRefreshOrder){
				this.getAll(1)
				this.getBuy(1)
				this.getEnd(1)
				this.getOff(1)
				this.$store.commit('works/SET_ISREFRESHORDER',false)
			}
		},
		watch:{
			keyword(val){
				this.$nextTick(()=>{
					this.keyword = this.$u.trim(val.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\ ]/g,''))
				})
			}
		},
		computed:{
			//是否刷新订单列表
			isRefreshOrder(){
				return this.$store.state.works.isRefreshOrder
			}
		},
		onReachBottom() {
			if(this.current == 0){
				if(this.allList.length < this.allTotal){
					this.allPage ++
					this.getAll()
				}
			}else if(this.current == 1){
				if(this.buyList.length < this.buyTotal){
					this.buyPage ++
					this.getBuy()
				}
			}else if(this.current == 2){
				if(this.endList.length < this.endTotal){
					this.endPage ++
					this.getEnd()
				}
			}else{
				if(this.offList.length < this.offTotal){
					this.offPage ++
					this.getOff()
				}
			}
		},
		methods:{
			goBack(){
				uni.switchTab({
					url:'/pages/tabBars/mine'
				})
			},
			cancel(item){
				this.modalShow = true
				this.cancelItem = item
			},
			//取消订单
			orderCancel(){
				this.$store.dispatch('wallet/orderCancel',{order_id:this.cancelItem.id}).then(res=>{
					this.$store.commit('wallet/SET_ISREFRESHWAITCOUNT',true)
					this.modalShow = false
					this.$refs.uModal.clearLoading()
					for(let i in this.allList){
						if(this.allList[i].id == this.cancelItem.id){
							this.allList.splice(i,1)
						}
					}
					for(let i in this.buyList){
						if(this.buyList[i].id == this.cancelItem.id){
							this.buyList.splice(i,1)
						}
					}
				}).catch(error=>{
					this.$refs.uModal.clearLoading()
				})
			},
			//待付款倒计时结束
			waitEnd(){
				this.getBuy(1)
				this.getAll(1)
				this.getOff(1)
				this.$store.commit('wallet/SET_ISREFRESHWAITCOUNT',true)
			},
			//全部订单倒计时结束
			allEnd(){
				this.getAll(1)
				this.getBuy(1)
				this.getOff(1)
				this.$store.commit('wallet/SET_ISREFRESHWAITCOUNT',true)
			},
			//计算过期时间
			getTime(time){
				let nowTime = parseInt(new Date().getTime() / 1000)
				let endTime = ''
				if(uni.getSystemInfoSync().platform == 'ios'){
					endTime = (new Date(time.replace(/-/g, '/')).getTime() / 1000 + 180)
				}else{
					endTime = (new Date(time).getTime() / 1000) + 180
				}
				if(nowTime - endTime >= 0){
					this.timestamp = 0//过期了
				}else{
					this.timestamp =  endTime - nowTime
				}
				return this.timestamp
			},
			searchValue(e){
				if (this.current == 0) {
					this.getAll(1)
				} else if (this.current == 1) {
					this.getBuy(1)
				} else if (this.current == 2) {
					this.getEnd(1)
				} else {
					this.getOff(1)
				}
			},
			change(index) {
				if (this.current != index) {
					this.current = index
					if (this.current == 0) {
						if(this.allTotal === ''){
							this.getAll()
						}
					} else if (this.current == 1) {
						if(this.buyTotal === ''){
							this.getBuy()
						}
					} else if (this.current == 2) {
						if(this.endTotal === ''){
							this.getEnd()
						}
					} else{
						if(this.offTotal === ''){
							this.getOff()
						}
					}
				}
			},
			//搜索全部订单
			getAll(val) {
				if(this.all_keyword != this.keyword || val == 1){
					this.allPage = 1
					this.allList = []
					this.allTotal = ''
				}
				this.allLoading = true
				this.allStatus = 'loading'
				this.$store.dispatch('wallet/order', { name: this.keyword , page:this.allPage}).then(res => {
					this.allList = [...this.allList,...res.data.data]
					this.allTotal = res.data.total
					this.all_keyword = this.keyword
					if(this.allList.length == this.allTotal && this.allList.length > 4){
						this.allStatus = 'nomore'
					}else{
						this.allLoading = false
					}
				})
			},
			//搜索待付款订单
			getBuy(val) {
				if(this.buy_keyword != this.keyword || val == 1){
					this.buyPage = 1
					this.buyList = []
					this.buyTotal = ''
				}
				this.buyLoading = true
				this.buyStatus = 'loading'
				this.$store.dispatch('wallet/order', { name: this.keyword ,status: 1 , page:this.buyPage}).then(res => {
					this.buyList = [...this.buyList,...res.data.data]
					this.buyTotal = res.data.total
					this.buy_keyword = this.keyword
					if(this.buyList.length == this.buyTotal && this.buyList.length > 4){
						this.buyStatus = 'nomore'
					}else{
						this.buyLoading = false
					}
				})
			},
			//搜索已完成订单
			getEnd(val) {
				if(this.end_keyword != this.keyword || val == 1){
					this.endPage = 1
					this.endList = []
					this.endTotal = ''
				}
				this.endLoading = true
				this.endStatus = 'loading'
				this.$store.dispatch('wallet/order', { name: this.keyword ,status: 2, page:this.endPage}).then(res => {
					this.endList = [...this.endList,...res.data.data]
					this.endTotal = res.data.total
					this.end_keyword = this.keyword
					if(this.endList.length == this.endTotal && this.endList.length > 4){
						this.endStatus = 'nomore'
					}else{
						this.endLoading = false
					}
				})
			},
			//搜索取消订单
			getOff(val) {
				if(this.off_keyword != this.keyword || val == 1){
					this.offPage = 1
					this.offList = []
					this.offTotal = ''
				}
				this.offLoading = true
				this.offStatus = 'loading'
				this.$store.dispatch('wallet/order', { name: this.keyword ,status: -1, page:this.offPage}).then(res => {
					this.offLoading = false
					this.offList = [...this.offList,...res.data.data]
					this.offTotal = res.data.total
					this.off_keyword = this.keyword
					if(this.offList.length == this.offTotal && this.offList.length > 4){
						this.offStatus = 'nomore'
					}else{
						this.offLoading = false
					}
				})
			},
			//再次购买，跳转市场详情
			againBuy(item){
				uni.navigateTo({
					url:`/pages/secondary/market/index?work_id=${item.work_id}`
				})
			},
			//前往订单详情
			toWait(item){
				//待付款订单
				if(item.status == 1){
					if(item.blind_box_id === 0){
						uni.navigateTo({
							url:`/pages/secondary/mine/order-wait?order_id=${item.id}`
						})
					}else{
						uni.navigateTo({
							url:`/pages/secondary/mine/order-wait?order_id=${item.id}`
						})
					}
				}else{
					//取消订单和已完成订单
					if(item.blind_box_id === 0){
						uni.navigateTo({
							url:`/pages/secondary/mine/order-cancel?created_at=${item.created_at}&price=${item.amount}&file=${item.works.file}&name=${item.works.name}&work_id=${item.work_id}&token_id=${item.works.token_id}&no=${item.market.no}&order_id=${item.id}&status=${item.status}&count=${item.count}&cover=${item.works.cover}&order_no=${item.order_no}`
						})
					}else{
						uni.navigateTo({
							url:`/pages/secondary/mine/order-cancel?created_at=${item.created_at}&price=${item.amount}&file=${item.box.cover_url}&name=${item.box.name}&work_id=${item.blind_box_id}&order_id=${item.id}&status=${item.status}&count=${item.count}&order_no=${item.order_no}`
						})
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		width: 100%;
		min-height: 100vh;
		background: #F8F9FB;
	}
	.slot-wrap {
		display: flex;
		align-items: center;
		flex: 1;
		// padding-right: 30upx;
		box-sizing: border-box;
		.search-wrap {
			width: 90%;
			margin: 0 20rpx 0 0;
			/deep/.u-action-active{
				position: absolute;
				right:100upx;
				width: 110upx;
			}
			/deep/.u-action-active:before{
				width: 1upx;
				height: 30upx;
				background: #999BA8;
				position: absolute;
				content: '';
				left:0;
				top:6upx;
			}
			/deep/.u-close-wrap{
				position: absolute;
				right:110px !important;
			}
			/deep/.u-input{
				padding-right: 85px;
			}
		}
			
		.filter-icon{
			width: 44upx;
			height: 44upx;
			image{
				width: 44upx;
				height: 44upx;
			}
		}
	}
	.search-box {
		width: 100%;
		padding: 30upx 30upx 0 30upx;
		box-sizing: border-box;
		background: #ffffff;
		.delete-icon {
			width: 30upx;
			height: 30upx;
	
			image {
				width: 30upx;
				height: 30upx;
				vertical-align: top;
			}
		}
	}
	
	.order-wrap{
		width: 100%;
		padding: 24upx;
		box-sizing: border-box;
		.order-item{
			width: 702upx;
			background: #FFFFFF;
			box-shadow: 0px 4upx 30upx -12upx rgba(102,105,120,0.2), 0px 4upx 12upx -6upx rgba(154,166,219,0.1);
			border-radius: 10upx;
			margin-bottom: 20upx;
			box-sizing: border-box;
			padding: 24upx 30upx 30upx 30upx;
			.transaction-time{
				margin-bottom: 16upx;
			}
			.pay-time{
				padding-right: 12upx;
				height: 32upx;
				background: #F5F6F9;
				border-radius: 18upx;
				.pay-tip{
					width: 117upx;
					height: 32upx;
					background: linear-gradient(129deg, #FDC035 0%, #FF9D00 100%);
					border-radius: 100upx 0px 74upx 100upx;
					margin-right:10upx;
					
				}
				.count-down-box{
					height: 32upx;
					display: flex;
					align-items: center;
				}
				/deep/.u-countdown-time{
					font-family: DIN !important;
					font-weight: bold !important;
				}
				/deep/.u-countdown-colon{
					line-height: 1em;
					font-size: 23upx;
					padding-bottom: 2upx !important;
				}
			}
			.order-img{
				width: 180upx;
				height: 180upx;
				border-radius: 8upx;
				margin-right: 18upx;
				image{
					width: 180upx;
					height: 180upx;
					border-radius: 8upx;
				}
			}
			.order-right{
				padding-top: 11upx;
				box-sizing: border-box;
				height: 180upx;
				width: 100%;
				.order-name{
					width: 57%;
					text-overflow: -o-ellipsis-lastline;
					overflow: hidden;				//溢出内容隐藏
					text-overflow: ellipsis;		//文本溢出部分用省略号表示
					display: -webkit-box;			//特别显示模式
					-webkit-line-clamp: 2;			//行数
					line-clamp: 2;					
					-webkit-box-orient: vertical;	//盒子中内容竖直排列
					word-wrap:break-word;
					word-break:break-all;
				}
				.order-amount{
					width: 41%;
					text-align: right;
					.amount{
						color: #2E2E4AFF;
					}
					.count{
						font-size: 28upx;
						color: #999BA8FF;
					}
				}
				.name-460{
					width: 460upx;
				}
				.order-price{
					bottom: 50upx;
				}
			}
			.btn{
				line-height: 58upx;
				width: 144upx;
				height: 58upx;
				border-radius: 29upx;
				margin-left: 24upx;
			}
		}
	}
	
</style>